package com.example.music.Controller;

import com.example.music.model.Music;
import com.example.music.model.User;
import com.example.music.service.LoveMusicService;
import com.example.music.tool.Constant;
import com.example.music.tool.ResponMessage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/lovemusic")
@RestController
public class LoveMusicController {
    @Autowired
    LoveMusicService loveMusicService;
    //收藏音乐功能

    @RequestMapping("/findLoveMusicByMusicIdAndUserId")
    ResponMessage<Boolean> findLoveMusicByMusicIdAndUserId(String musicid, HttpSession httpSession) {
        if (httpSession == null ||
                httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录！");
            return new ResponMessage<>(-1, "没有登录！", false);
        }
        int userId = ((User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY)).getId();
        int musicId = Integer.parseInt(musicid);

        Music music = loveMusicService.findLoveMusicByMusicIdAndUserId(userId, musicId);
        if (music != null) {
            return new ResponMessage<>(-1, "您收藏过了", false);
        } else {
            boolean flag = loveMusicService.insertLoveMusic(userId, musicId);
            if (flag) {
                return new ResponMessage<>(1, "收藏成功", true);
            } else {
                return new ResponMessage<>(-1, "收藏失败", false);
            }
        }
    }
    //根据收藏音乐名查询音乐功能（有参数，和无参数）

    @RequestMapping("/findMusic")
    ResponMessage<List<Music>> findMusic(@RequestParam(required = false) String musicName,
                                         HttpServletRequest req, HttpSession httpSession) {
        if (httpSession == null ||
                httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录！");
            return new ResponMessage<>(-1, "没有登录", null);
        }
        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userid = user.getId();

        List<Music> musicList = null;
        if (musicName == null) {
            musicList = loveMusicService.findLoveMusicByUserId(userid);
        } else {
            musicList = loveMusicService.findLoveMusicBykeyAndUID(musicName, userid);
        }
        return new ResponMessage<>(1, "查询成功", musicList);
    }

//取消收藏功能
    @RequestMapping("/deletelovemusic")
    public ResponMessage<Boolean> deleteLoveMusic(@RequestParam String id,
                                                  HttpServletRequest req) {
        HttpSession httpSession = req.getSession(false);
        if (httpSession == null ||
                httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录！");
            return new ResponMessage<>(-1, "没有登录", null);
        }
        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userid = user.getId();
        Boolean did=loveMusicService.deleteLoveMusic(Integer.parseInt(id),userid);
        if(did){
            return new ResponMessage<>(1, "取消收藏成功", true);
        }
        return new ResponMessage<>(-1, "取消收藏失败", false);
    }
}






